import React, {useState, useReducer} from 'react'
// redux必须的内容
// store reducer

// useReducer使用方法
// 1. 需要创建 数据仓库 和 管理者
// 2. 通过useReducer(reducer, store)来获取state和dispatch
const store = {
  num: 10
}

const reducer = (state, action) => {
  switch (action.type) {
    case 'changeNum':
      return {
        ...state,
        num: action.num
      }
    default:
      return {
        ...state
      }
  }
}

function StateFunction() {
  const [state,dispatch] = useReducer(reducer, store)
  const [num, setNum] = useState(1)

  return <div onClick={()=>{
    dispatch({
      type: 'changeNum',
      num: 100
    })
  }}>
    这是一个函数组件-{state.num}
  </div>
}

export default StateFunction